package controle.turma;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modelagem.Turma;
import controle.conexao;
import controle.Curso.cursoDAO;
import controle.professor.professorDAO;

public class turmaDAO {
	public long inserir(Turma turma) {
		long id = -1;
		
				
		String sql = "insert into cont_academico."
				+ "turma(id_prof, codigo, id_curso, alunos) "
				+ "values(?,?,?,'"+turma.getAlunos()+"')";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql,
					PreparedStatement.RETURN_GENERATED_KEYS);
			
			statement.setInt(1, turma.getId_prof());
			statement.setString(2, turma.getCodigo());
			statement.setInt(3, turma.getId_curso());
			
			statement.executeUpdate();
			ResultSet set = statement.getGeneratedKeys();
			
			while (set.next()) {
				id = set.getLong("id");
			}
			JOptionPane.showMessageDialog(null, "Salvo com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return id;
	}
	
	
	
	public List<Turma> buscar(){
		String sql;
		Turma turma;
		List<Turma> lista = new ArrayList<Turma>();
			sql = "select * from cont_academico."
				+ "turma order by id_prof;";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.execute();
			ResultSet set = statement.getResultSet();
			
			while (set.next()) {
				turma = new Turma();
				turma.setId(set.getInt("id"));
				turma.setId_prof(set.getInt("id_prof"));
				turma.setId_curso(set.getInt("id_curso"));
				turma.setCodigo(set.getString("codigo"));
				
				turma.setProfNome(new professorDAO().buscarPorid(turma.getId_prof()).getNome());
				turma.setCursoNome(new cursoDAO().buscarPorId(turma.getId_curso()).getNome());
				lista.add(turma);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return lista;
	}
	
	
	
	public void atualizar(Turma turma){
		String sql = "update cont_academico."
				+ "turma set (id_prof, codigo, id_curso, alunos)" 
				+ "= (?,?,?) where id = ?";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, turma.getId_curso());
			statement.setInt(2, turma.getId_prof());
			statement.setInt(5, turma.getId());
			statement.execute();
			
			JOptionPane.showMessageDialog(null, "Atualizado com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}
	
	public void remover(Turma turma){
		String sql = "delete from cont_academico."
				+ "turma where id = ?;";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, turma.getId());
			statement.execute();
			
			JOptionPane.showMessageDialog(null, "Atualizado com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}

}
